﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace GUI.CongTy
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private DateTime date;
        private string MaCongTy = string.Empty;
        private string links = string.Empty;
        private string linksRoot = string.Empty;
        private Image image;
        private byte[] bytes;

        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private Entities.CongTy congty;

        public Entities.CongTy Congty
        {
            get { return congty; }
            set { congty = value; }
        }
        public frmXuLy()
        {
            InitializeComponent();
        }
        public frmXuLy(string hanhDong, Entities.CongTy CongTy)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.Congty = CongTy;
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            try
            {
                date = Common.StaticVariable.InterfaceObject.SelectDate();
                switch (this.hanhDong)
                {
                    case "Them":
                        MaCongTy = this.GetID();
                        break;
                    case "Sua":
                        DoDuLieu();
                        break;
                    default:
                        this.Thoat();
                        break;
                }
                this.WindowState = FormWindowState.Normal;
                txtTenCongTy.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua Du Lieu
        private void DoDuLieu()
        {
            try
            {
                MaCongTy = congty.MaCongTy;
                txtTenCongTy.Text = congty.TenCongTy;
                txtDiaChi.Text = congty.DiaChi;
                txtEmail.Text = congty.Email;
                txtWebsite.Text = congty.Website;
                maskedTextBox2.Text = congty.DienThoai;
                txtfax.Text = congty.Fax;
                textBox5.Text = congty.LinkImageRoot;
                if (this.congty.LinkImageRoot != string.Empty)
                {
                    bytes = Common.StaticVariable.InterfaceObject.GetImage(congty.LinkImageRoot);
                    if (bytes != null)
                    {
                        image = Common.Utilities.GetImage(bytes);
                        pictureBox1.BackgroundImage = image;
                    }
                    else
                    {
                        bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                        image = Common.Utilities.GetImage(bytes);
                        pictureBox1.BackgroundImage = image;
                    }
                }
                else
                {
                    bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                    image = Common.Utilities.GetImage(bytes);
                    pictureBox1.BackgroundImage = image;
                }
                textBox8.Text = congty.GhiChu;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Lay Ma
        private string GetID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "CongTy";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "CT_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "CT_0001";
            }
            return code;
        }
        #endregion


        #region Quyen
        private Boolean Quyen()
        {
            return true;
        }
        #endregion

        #region Gia Tri Them
        private Entities.CongTy LayBanGhi(string hanhdong)
        {
            Entities.CongTy nv = new Entities.CongTy();
            try
            {
                nv.HanhDong = hanhdong;
                nv.MaCongTy = MaCongTy;
                nv.TenCongTy = txtTenCongTy.Text.Trim();
                nv.DiaChi = txtDiaChi.Text ;
                nv.Email = txtEmail.Text;
                nv.Website = txtWebsite.Text;
                nv.DienThoai = maskedTextBox2.Text;
                nv.Fax = txtfax.Text;
                nv.LinkImage = links;
                nv.LinkImageRoot = linksRoot;
                nv.Images = bytes;
                nv.GhiChu = textBox8.Text;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                nv = null;
            }
            return nv;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.CongTy nv)
        {
            Boolean kt = true;
            try
            {
                if (nv.MaCongTy == string.Empty)
                {
                    MessageBox.Show("Chưa có mã công ty", "Thông báo");
                    kt = false;
                    return kt;
                }
                if (nv.TenCongTy == string.Empty)
                {
                    MessageBox.Show("Nhập tên công ty", "Thông báo");
                    txtTenCongTy.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.DiaChi == string.Empty)
                {
                    MessageBox.Show("Nhập địa chỉ", "Thông báo");
                    txtDiaChi.Focus();
                    kt = false;
                    return kt;
                }
                if (Common.ValidateControl.IsEmail(nv.Email) == false)
                {
                    MessageBox.Show("Định dạng email không đúng", "Thông báo");
                    txtEmail.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.Website == string.Empty)
                {
                    MessageBox.Show("Nhập website", "Thông báo");
                    txtWebsite.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.DienThoai == string.Empty)
                {
                    MessageBox.Show("Nhập điện thoại", "Thông báo");
                    maskedTextBox2.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.Fax == string.Empty)
                {
                    MessageBox.Show("Nhập fax", "Thông báo");
                    txtfax.Focus();
                    kt = false;
                    return kt;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = false;
            }
            return kt;
        }
        #endregion

        #region Tim Anh
        private void Search()
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    links = open.SafeFileName.ToString();
                    linksRoot = open.FileName.ToString();
                    bytes = Common.Utilities.GetImage(linksRoot);
                    image = Common.Utilities.GetImage(bytes);
                    pictureBox1.BackgroundImage = image;
                    textBox5.Text = linksRoot;
                    pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                }
                else
                {
                    links = this.congty.LinkImage;
                    linksRoot = this.congty.LinkImageRoot;
                    textBox5.Text = this.congty.LinkImage;
                    bytes = this.congty.Images;
                    image = this.congty.Picture;
                }
                textBox8.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", "Thông báo");
                    return;
                }
                Entities.CongTy nv = this.LayBanGhi("Insert");
                this.MaCongTy = this.GetID();
                if (nv.MaCongTy != this.MaCongTy)
                {
                    nv = this.LayBanGhi("Insert");
                }
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateCongTy(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền sửa", "Thông báo");
                    return;
                }
                Entities.CongTy nv = this.LayBanGhi("Update");
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateCongTy(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = false;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.hanhDong)
                {
                    case "Them":
                        this.Them();
                        break;
                    case "Sua":
                        this.Sua();
                        break;
                }
            }
            catch (Exception)
            {

            }
        }

        private void txtTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckNumber(e);
        }

        private void txtDienThoaiBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            this.Search();
        }
    }
}
